#include <linux/input.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>

#define KEY_EVENT_DEV_NAME "/dev/input/event0"

int sys_key_scan(void)
{
    int ret = -1;
    int i = 0;
    int key_fd = 0;
    struct input_event key_event = {0};

    key_fd = open(KEY_EVENT_DEV_NAME, O_RDONLY);
    if (key_fd <= 0) {
        printf("open event device error!\n");
        return ret;
    }

    while (1) {
        ret = lseek(key_fd, 0, SEEK_SET);
        ret = read(key_fd, &key_event, sizeof(key_event));

        if (ret) {
            if (key_event.type == EV_KEY && (key_event.value == 0 || key_event.value == 1)) {
                printf("key %d %s\n", key_event.code, (key_event.value) ? "pressed": "released");

                if (key_event.code == KEY_ESC)
                    break;
            }
        }
    }

    close(key_fd);

    return ret;
}

int main(int argc, char ** argv)
{
    printf("This is a key event test!\n");
    sys_key_scan();
}
